<?
	include_once('header.php');

	include_once('sidebar.php');

	$action = $_GET['action'];
	$this_item = $_GET['item'];
	if(get_magic_quotes_gpc()) {
		$this_item = stripslashes($this_item); 
	}
?>

		
		<div id="main">
			
			<h2>Media Management<? if(isset($_GET['list'])) { echo ": " . ucwords($_GET['list']); } ?></h2>
<?
	if(!drizzo_user_is_allowed('gallery-write') || drizzo_global_options('module_gallery') != 1) {
		echo "<br class=\"clear\" /><p>This module is not available.</p><br class=\"clear\" />";
		include_once('footer.php');
		exit();
	}
?>

			<br class="clear" />
<?
	if($action == 'add') {
		if(count($_POST) > 0) {
			echo drizzo_media_save();
		}
?>
			<form action="" method="post" enctype="multipart/form-data">
			<label for="save_location">Save to</label> <? echo drizzo_upload_directories_dropdown(); ?><br class="clear" />
			<label for="new_folder">New Folder<a href="javascript:void(0)" title="Help">?<span class="explain">Enter a name for a new folder which will be created under the folder selected above. IE, if you enter 'folder-a' here and 'gallery' is selected above, the new folder will be at 'gallery/folder-a'.</span></a></label> <input type="text" name="new_folder" /><br class="clear" />
			<input type="hidden" name="MAX_FILE_SIZE" value="83886080" />
			<label for="file">File</label> <input type="file" name="file" id="media_file"/><br class="clear" />
			<div id="gallery_meta" style="display: none;">
				<label for="thumbnail">Thumbnail</label> <input type="file" name="thumbnail" /><br class="clear" />
				<hr class="rule" />
				<label for="auto_thumb">Or Create Thumbnail Automatically</label> <select name="auto_thumb"><option value="0" onclick="new Effect.BlindUp('thumb_size')">No</option><option value="1" onclick="new Effect.BlindDown('thumb_size')">Yes</option></select><br class="clear" />
				<div id="thumb_size" style="display:none;">
					<label for="thumb_width">Thumbnail Width</label> <input type="text" name="thumb_width" value="<? echo drizzo_global_options('gallery_thumb_width'); ?>" /><br class="clear" />
					<label for="thumb_height">Thumbnail Height</label> <input type="text" name="thumb_height" value="<? echo drizzo_global_options('gallery_thumb_height'); ?>" /><br class="clear" />
				</div>
				<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"></textarea><br />
			
				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>
			</div>
			<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="sumbit" value="Upload File" class="submit" /></p>
<?
	} elseif($action == 'create_thumb') {
		$img_link = domain . str_replace(root_dir,"",$this_item);
		$thumb_link = domain . str_replace(root_dir,"",str_replace("gallery","gallery/thumbs",$this_item));
		if(count($_POST) > 0) {
			echo drizzo_media_replace();
		} else {
			if(file_exists(str_replace("gallery","gallery/thumbs",$this_item))) {
?>
			<form action="" method="post">
				<input type="hidden" name="MAX_FILE_SIZE" value="83886080" />
				<label for="this_image">Fullsize Image</label> <img src="<? echo $img_link; ?>" alt="Fullsize Image" /><br class="clear" />
				<label for="current_thumb">Current Thumbnail</label> <img src="<? echo $thumb_link; ?>" alt="Thumbnail" /><br class="clear" />
				<label for="new_file">New Thumbnail</label> <input type="file" name="new_file" /><br class="clear" />
				<hr class="rule" />
				
				<label for="make_new_thumb">Or Create Thumbnail Automatically</label> <select name="make_new_thumb"><option value="0">No</option><option value="1">Yes</option></select><br class="clear" />
				<label for="thumb_width">Thumbnail Width</label> <input type="text" name="thumb_width" value="<? echo drizzo_global_options('gallery_thumb_width'); ?>" /><br class="clear" />
				<label for="thumb_height">Thumbnail Height</label> <input type="text" name="thumb_height" value="<? echo drizzo_global_options('gallery_thumb_height'); ?>" /><br class="clear" />
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="sumbit" value="Create Thumbnail" class="submit" /></p>
			</form>
<?				
			} else {
?>
			<form action="" method="post">
				<input type="hidden" name="MAX_FILE_SIZE" value="83886080" />
				<label for="this_image">Fullsize Image</label> <img src="<? echo $img_link; ?>" alt="Fullsize Image" /><br class="clear" />
				<label for="new_file">New Thumbnail</label> <input type="file" name="new_file" /><br class="clear" />
				<hr class="rule" />
				
				<label for="make_new_thumb">Or Create Thumbnail Automatically</label> <select name="make_new_thumb"><option value="0">No</option><option value="1">Yes</option></select><br class="clear" />
				<label for="thumb_width">Thumbnail Width</label> <input type="text" name="thumb_width" value="<? echo drizzo_global_options('gallery_thumb_width'); ?>" /><br class="clear" />
				<label for="thumb_height">Thumbnail Height</label> <input type="text" name="thumb_height" value="<? echo drizzo_global_options('gallery_thumb_height'); ?>" /><br class="clear" />
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="sumbit" value="Create Thumbnail" class="submit" /></p>
			</form>
<?
			}
		}
	} elseif($action == 'move') {
		if(count($_POST) > 0) {
			echo drizzo_media_update();
		} else {
			if(is_file($this_item)) {
				$fileinfo = pathinfo($this_item);
				$filename = str_replace("." . $fileinfo['extension'],"",$fileinfo['basename']);
				$foldername = str_replace(root_dir . "drizzo-files/","",dirname($this_item));
				if(drizzo_media_is_file_type('image',$this_item)) {
					echo "\t<img src=\"" . domain . str_replace(root_dir,"",$this_item) . "\" alt=\"" . basename($this_item) . "\" />";
				}
			} elseif(is_dir($this_item)) {
				$filename = basename($this_item);
				$foldername = str_replace(root_dir . "drizzo-files","",$this_item);
			}
?>
	<form action="" method="post">
		<label for="current_location">Current Location</label> <div class="faux_input"><? echo $foldername; ?></div><br class="clear" />
		<label for="save_location">Move to</label> <? echo drizzo_upload_directories_dropdown(); ?><br class="clear" />
		<label for="new_folder">New Folder<a href="javascript:void(0)" title="Help">?<span class="explain">Enter a name for a new folder which will be created under the folder selected above. IE, if you enter 'folder-a' here and 'gallery' is selected above, the new folder will be at 'gallery/folder-a'.</span></a></label> <input type="text" name="new_folder" /><br class="clear" />
	<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="sumbit" value="Move" class="submit" /></p>
	</form>
<?	
		}
	} elseif($action == 'rename') {
		if(count($_POST) > 0) {
			echo drizzo_media_update();
		} else {
			if(is_file($this_item)) {
				$fileinfo = pathinfo($this_item);
				$filename = str_replace("." . $fileinfo['extension'],"",$fileinfo['basename']);
				if(drizzo_media_is_file_type('image',$this_item)) {
					echo "\t<img src=\"" . domain . str_replace(root_dir,"",$this_item) . "\" alt=\"" . basename($this_item) . "\" />";
				}
			} elseif(is_dir($this_item)) {
				$filename = basename($this_item);
			}
?>
			<form action="" method="post">
				<label for="name">File Name</label> <input type="text" name="name" value="<? echo $filename; ?>" /><br class="clear" />
				<input type="hidden" name="save_location" value="<? echo dirname(str_replace(root_dir,'',$this_item)); ?>" />
			<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="sumbit" value="Rename" class="submit" /></p>
			</form>
<?	
		}
	} elseif($action == 'replace') {
		if(count($_POST) > 0) {
			echo drizzo_media_replace();
		} else {
			if(drizzo_media_is_file_type('image',$this_item)) {
				echo "\t<img src=\"" . domain . str_replace(root_dir,"",$this_item) . "\" alt=\"" . basename($this_item) . "\" />";
			}
?>			
			<form action="" method="post">
				<input type="hidden" name="MAX_FILE_SIZE" value="83886080" />
				<label for="new_file">New File</label> <input type="file" name="new_file" /><br class="clear" />
<?
			if(strpos($this_item,"thumbs") !== false) {
?>
				<hr class="rule" />
				<label for="make_new_thumb">Or Create Thumbnail Automatically</label> <select name="make_new_thumb"><option value="0">No</option><option value="1">Yes</option></select><br class="clear" />
				<label for="thumb_width">Thumbnail Width</label> <input type="text" name="thumb_width" value="<? echo drizzo_global_options('gallery_thumb_width'); ?>" /><br class="clear" />
				<label for="thumb_height">Thumbnail Height</label> <input type="text" name="thumb_height" value="<? echo drizzo_global_options('gallery_thumb_height'); ?>" /><br class="clear" />
<?
			}
?>
			<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="sumbit" value="Replace" class="submit" /></p>
			</form>
<?
		}
	} elseif($action == 'delete') {
		if(count($_POST) > 0) {
			echo drizzo_media_delete();
		} else {
			if(basename($this_item) == 'cache' || basename($this_item) == 'thumbs') {
				echo "<p class=\"alert\">This folder cannot be deleted.</p>";
			} elseif(is_file($this_item)) {
				$fileinfo = pathinfo($this_item);
				$filename = str_replace("." . $fileinfo['extension'],"",$fileinfo['basename']);
				if(drizzo_media_is_file_type('image',$this_item)) {
					echo "\t<img src=\"" . domain . str_replace(root_dir,"",$this_item) . "\" alt=\"" . basename($this_item) . "\" />";
				}
			} elseif(is_dir($this_item)) {
				$filename = basename($this_item);
			}
?>
			<form action="" method="post">
			<p><strong>Are you sure you want to delete "<? echo $filename . "\""; if(is_dir($this_item)) { echo " and all its contents";} ?>?</strong></p>
			<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="sumbit" value="Delete" class="submit" /></p>
			</form>
<?	
		}
	}
	if(count($_GET) == 0 || ($action != null && $action != 'add' && count($_POST) > 0) || ($_GET['list'] && count($_GET) == 1)) {
		if(isset($_GET['list'])) {
			echo drizzo_list_folder_contents($_GET['list']);
		} else {
			echo drizzo_list_folder_contents();
		}
	}
?>			
			<br class="clear" />
			
		</div>
<?
	include_once('footer.php');
?>
